package com.dyzs.activity;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

import com.dyzs.util.Constants;
import com.dyzs.util.HttpDownloader;
import com.dyzs.util.ReadProperties;

public class CommonActivity extends Activity {
	
	protected void savaInitParams(Map map){  
		 if(map!=null&&!map.isEmpty()){
			 SharedPreferences settings = getSharedPreferences(Constants.SHARE_LOGIN_TAG, 0); 
			 SharedPreferences.Editor edit = settings.edit();
			 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
			      Map.Entry e = (Map.Entry) it.next();
			      edit.putString(e.getKey().toString(), e.getValue().toString());
			 }
			 edit.commit();
		 }
	 }
	
	/**
	 * ������
	 * @param map
	 * @return
	 */
	protected Map getInitParams(){
		SharedPreferences settings = getSharedPreferences(Constants.SHARE_LOGIN_TAG, 0); 
		return settings.getAll();
	}
	
	
	/**
	 * ���keyɾ���ʼ�����
	 * @param map
	 */
	protected void removeInitParams(String key){  
		 if(key!=null&&!"".equals(key)){
			 SharedPreferences settings = getSharedPreferences(Constants.SHARE_LOGIN_TAG, 0); 
			 SharedPreferences.Editor edit = settings.edit();
			 edit.remove(key);
			 edit.commit();
		 }
	 }
	
	
	
	
	
	
	protected void showDialog(final Context context,String appName,final String programname) {   
        AlertDialog.Builder builder = new AlertDialog.Builder(context);   
//        builder.setIcon(R.drawable.icon);   
        builder.setTitle("提示");   
        builder.setMessage("没有安装"+appName+",是否下载?");   
        builder.setPositiveButton("确定",   
                new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                    	if(Constants.LOGIN_NONET){
							Toast.makeText(context, "离线登录不提供安装下载",Toast.LENGTH_SHORT).show();	
						}else{
							if(!checkNet()){
								Toast.makeText(context,"网络异常，请重试",Toast.LENGTH_SHORT).show();
							}else{
								programName=programname;
								showProgressDialog(getString(R.string.down_title), getString(R.string.down_content), true, true);
								Thread loginThread = new Thread(new fileDownThread());
								loginThread.start();
							}
						}
                    }   
                });              
        builder.setNegativeButton("取消",   
                new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {                        
                    }   
                });   
        builder.show();   
    }   

	String programName="";	
	class fileDownThread implements Runnable {		 
		public void run() {
			HttpDownloader httpDownloader = new HttpDownloader();										
			int result = httpDownloader.downFileInSd(Constants.SERVER_URL+"/templateFile/"+programName, ReadProperties.getPropertyByStr("client.filepaht"), programName);
			Message msg = mainHandler.obtainMessage();								
			msg.arg1=result;
			mainHandler.sendMessage(msg);				
		}	
	}
	
	Handler mainHandler = new Handler() {
		public void handleMessage(Message msg) {
			proDialog.dismiss();
			if(msg.arg1==0){
				String filePath=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator+ ReadProperties.getPropertyByStr("client.filepaht") + File.separator + programName;
				Intent i = new Intent(Intent.ACTION_VIEW); 						   
				i.setDataAndType(Uri.fromFile(new File(filePath)),"application/vnd.android.package-archive"); 
				startActivity(i);
			}else if(msg.arg1==1){
				Toast.makeText(CommonActivity.this, getResources().getString(R.string.no_sd),Toast.LENGTH_SHORT).show();
			}else if(msg.arg1==-1){
				Toast.makeText(CommonActivity.this, "下载失败",Toast.LENGTH_SHORT).show();
			}			
		}
	};
	
	
	public ProgressDialog proDialog;	
	protected void showProgressDialog(String title,String content,boolean bl1,boolean bl2) {
//		proDialog.setIcon(icon);//设置标题图标
//		proDialog.setCancelable(true);//是否可以按退回按钮取消
//		proDialog.setIndeterminate(false);//进度条是否不明确
		proDialog = ProgressDialog.show(this,null, content, bl1, bl2);
	}
	
	
	
	
	
	

	
	
	
	
	
	protected void backClient(){
		new AlertDialog.Builder(this).setTitle("退出确认").setIcon(android.R.drawable.ic_menu_revert).setMessage(
        "您确定要退出么?").setPositiveButton("确定",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {             	
//            	Intent intent = new Intent(CommonActivity.this, LoginActivity.class);
//        		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        		startActivity(intent);
        		android.os.Process.killProcess(android.os.Process.myPid()); 
//        		finish();
//            	System.exit(0);
            }
        }).setNegativeButton("取消",null).show();
	}
	
	
	
	
	protected Map<String,Object> getDisplay(){
		WindowManager windowManager = getWindowManager();
	    Display display = windowManager.getDefaultDisplay();
	    Map map=new HashMap<String,Object>();
	    map.put("width", display.getWidth());
	    map.put("height", display.getHeight());
	    return map;
	}
	
	
	
	
	
	protected  boolean checkNet() {
		ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		boolean b=cwjManager.getActiveNetworkInfo().isAvailable();
		return b;
 	} 
}
